using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.Data.Entity.Sys;
using net_work.Data.VO.Sys;
using net_work.IRepository.Sys;
using net_work.IService.Sys;

namespace net_work.Service.Sys;

public class AdministrativeClassService(
    IAdministrativeClassRepository administrativeClassRepository,
    ICollegeRepository collegeRepository,
    ITeacherRepository teacherRepository)
    : IAdministrativeClassService
{
    public Task<PagedResult<AdministrativeClassResult>> GetAdministrativeClasssPagedAsync(PagedRequest request)
    {
        return administrativeClassRepository.GetAdministrativeClassPagedAsync(request);
    }

    public Task<AdministrativeClass?> SelectByIdAsync(long administrativeClassId)
    {
        return administrativeClassRepository.GetByIdAsync(administrativeClassId);
    }

    public async Task<long> InsertAdministrativeClass(AdministrativeClassAddRequest administrativeClassAddRequest)
    {
        var college = await collegeRepository.GetByIdAsync(administrativeClassAddRequest.CollegeId);
        if (college == null)
        {
            return -1;
        }

        var teacher = await teacherRepository.GetByIdAsync(administrativeClassAddRequest.HeadTeacherId);
        if (teacher == null)
        {
            return -1;
        }

        return await administrativeClassRepository.InsertAsync(administrativeClassAddRequest);
    }

    public async Task<int> UpdateAdministrativeClass(AdministrativeClassUpdateRequest administrativeClassUpdateRequest)
    {
        if (administrativeClassUpdateRequest.Id <= 0)
        {
            return -1;
        }

        var college = await collegeRepository.GetByIdAsync(administrativeClassUpdateRequest.CollegeId);
        if (college == null)
        {
            return -1;
        }

        var teacher = await teacherRepository.GetByIdAsync(administrativeClassUpdateRequest.HeadTeacherId);
        if (teacher == null)
        {
            return -1;
        }

        return await administrativeClassRepository.UpdateAsync(administrativeClassUpdateRequest) ? 1 : -1;
    }

    public async Task<int> DeleteAdministrativeClassByIds(List<long> ids)
    {
        return await administrativeClassRepository.DeleteByIdsAsync(ids) ? 1 : -1;
    }
}